import Vue from 'vue'
import VueRouter from 'vue-router'
import findLast from 'lodash/findLast'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {notification} from 'ant-design-vue'
import {check, isLogin} from '../utils/auth'

Vue.use(VueRouter)

const routes = [{
    path: '/user',
    hideInMenu: true,
    component: () => import( /* webpackChunkName: "layout" */ '../layouts/UserLayout.vue'),
    name: 'user',
    children: [{
        path: '/user',
        redirect: '/user/login'
      },
      {
        path: '/user/login',
        name: 'login',
        component: () => import( /* webpackChunkName: "user" */ '../views/User/Login.vue')
      },
      {
        path: '/user/register',
        name: 'register',
        component: () => import( /* webpackChunkName: "user" */ '../views/User/Register.vue')
      }
    ]
  },
  {
    path: "/",
    meta: {
      authority: ["user", "admin"]
    },
    component: () =>
      import( /* webpackChunkName: "layout" */ "../layouts/BasicLayout"),
    children: [
      // dashboard
      {
        path: "/",
        redirect: "/dashboard/analysis"
      },
      {
        path: "/dashboard",
        name: "dashboard",
        meta: {
          icon: "dashboard",
          title: "仪表盘"
        },
        component: {
          render: h => h("router-view")
        },
        children: [{
          path: "/dashboard/analysis",
          name: "analysis",
          meta: {
            title: "分析页"
          },
          component: () =>
            import( /* webpackChunkName: "dashboard" */ "../views/Dashboard/Analysis")
        }]
      },
      // form
      {
        path: "/form",
        name: "form",
        component: {
          render: h => h("router-view")
        },
        meta: {
          icon: "form",
          title: "表单",
          authority: ["admin"]
        },
        children: [{
            path: "/form/basic-form",
            name: "basicform",
            meta: {
              title: "基础表单"
            },
            component: () =>
              import( /* webpackChunkName: "form" */ "../views/Forms/BasicForm")
          },
          {
            path: "/form/step-form",
            name: "stepform",
            hideChildrenInMenu: true,
            meta: {
              title: "分布表单"
            },
            component: () =>
              import( /* webpackChunkName: "form" */ "../views/Forms/StepForm"),
            children: [{
                path: "/form/step-form",
                redirect: "/form/step-form/info"
              },
              {
                path: "/form/step-form/info",
                name: "info",
                component: () =>
                  import( /* webpackChunkName: "form" */ "../views/Forms/StepForm/Step1")
              },
              {
                path: "/form/step-form/confirm",
                name: "confirm",
                component: () =>
                  import( /* webpackChunkName: "form" */ "../views/Forms/StepForm/Step2")
              },
              {
                path: "/form/step-form/result",
                name: "result",
                component: () =>
                  import( /* webpackChunkName: "form" */ "../views/Forms/StepForm/Step3")
              }
            ]
          }
        ]
      }
    ]
  },
  {
    path: '*',
    component: () =>
      import( /* webpackChunkName: "notFound" */ "../views/404"),
    name: '404',
    hideInMenu: true
  },
  {
    path: '/403',
    component: () =>
      import( /* webpackChunkName: "notFound" */ "../views/403"),
    name: '403',
    hideInMenu: true
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path != from.path) { //去掉自身 域名跳转时候的进度条
    NProgress.start()
  }
  //拿到当前路由最近的路由权限
  const record = findLast(to.matched, record => record.meta.authority)
  if(record && !check(record.meta.authority)){ //判断是否有权限查看此路由
    if(!isLogin() && to.path != '/user/login'){ //判断是否已登录
      next({path:'/user/login'}) // 未登录跳转登录
    }
    else if(to.path != '/403') {
      notification.error({
        message:'403',
        description:'你没有权限访问，请联系管理员咨询。'
      })
      next({path:'/403'}) // 已登录跳转无权限
    }
    NProgress.done()
  }
  //加载动画开始
  next()

})
router.afterEach(() => {
  NProgress.done() //加载动画结束
})

export default router